home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14336 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.2 KB  |  118 lines

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: MSVC 1.5 problem using CString class and fstream
  5. Date: 29 Mar 1996 18:14:20 GMT
  6. Organization: Netcom
  7. Message-ID: <4jh99s$1gp@dfw-ixnews6.ix.netcom.com>
  8. References: <alanshepherd.28.000A4801@online.rednet.co.uk>
  9. NNTP-Posting-Host: den-co10-17.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Fri Mar 29 12:14:20 PM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <alanshepherd.28.000A4801@online.rednet.co.uk>, 
  16. alanshepherd@online.rednet.co.uk says...
  17. >
  18. >Hello I am a newby to C++, MSVC and this group so please forgive if this 
  19. >question is naive or has been posted previously.
  20. >
  21. >I am having problems in two areas and I suspect they are related. The first 
  22. >occours when I tried to write a simple C++ program using CString types. A
  23. >simple quickwin example is shown below.
  24. >
  25. >#include <iostream.h>
  26. >#include<afx.h>
  27. >
  28. >int CStringfunc( CString&, CString&);
  29. >
  30. >CString line = "this is a test of CStringfunc";
  31. >CString word = "test";
  32. >
  33. >void main()
  34. >{
  35. >     int i = CStringfunc(line,word);
  36. >    cout << " i = " << i << '\n';
  37. >}
  38. >.
  39. >definition of CStringfunc etc.
  40. >.
  41. >
  42. >The code compiles ok but the linker complains that .."mlibcew(delete.cxx) : 
  43. >error l2044: void __far __cdecl operator delete(void __near*) symbol multiply 
  44. >defined" followed by some advice on relinking with /NOE (which also does not 
  45. >work. The same message is repeated for "operator new"
  46. >
  47. >this is followed by a list of L2029 errors (unresolved externals) including;
  48. >GETFILETITLE...DRAGACCEPTFILES...CHOOSECOLOR etc. I have experimented with
  49. >including various libraries in the link options with no sucess. Any sugestions?
  50. >
  51. >The second problem is similar but comes at it from the oposite direction. In 
  52. >this case I wish to use <fstream.h> in my appwizzard generated widows program. 
  53. >I wanted to do some simple text file i/o and thought the fstream class gave 
  54. >better service than the CStdiofile class. The work was just some anciliary 
  55. >bookkeeping and so I didnt want to go the serialise/archive route. I include 
  56. >the header file references for example
  57. >
  58. >#include "stdafx.h"
  59. >#include "appro.h"
  60. >#include "project.h"
  61. >#include "logon.h"
  62. >
  63. >#include "fstream.h"
  64. >#include "direct.h"
  65. >#include "sys\types.h"
  66. >#include "sys\stat.h"
  67. >.
  68. >.
  69. >Again the code compiles ok but does not link with exactly the same L2044 error 
  70. >on the 'new' and 'delete' operators. I have written a seperate small program 
  71. >using fstream it works fine UNTILL I include <afx.h>. Are these seperate ways 
  72. >of working incompatable or am I doing something wrong? I cant seem to use the 
  73. >CString class outside the application framework or the fstream class within.
  74. >If anyone else has had similar problems to this or any advice at all I would 
  75. >be greatful to hear from them.
  76.  
  77. For what it's worth, I've been able to use fstream inside an MFC app
  78. just fine, but that is MSVC++4.0.
  79.  
  80. There are two different problems here.  The first has to do with
  81. multiply-defined operators, which I have seen before and worked
  82. around by excluding linkage of the "msvcrt40" library.  That is
  83. for MSVC4.0, you may have a different version and I don't know
  84. what works for older VC++.
  85.  
  86. For the second problem (all the undefined symbols)
  87. It sounds like the problem originates with the <afx.h> header.
  88. Many headers, like <ios.h>, define a file-static class variable
  89. whose sole purpose is to ensure that some subsystem has been
  90. initialized prior to startup.  This is usually done via a lightweight
  91. reference-counting object:
  92.  
  93. // Inside someHeader.h:
  94.     // always zero at link-time
  95.     extern static int TheRefCount;
  96.     class ref {
  97.        ref() { 
  98.           if (theRefCount==0) 
  99.              otherClass::initSubsystem(); 
  100.           theRefCount++;
  101.        }
  102.     };
  103.     static ref localRef;
  104.  
  105. These local objects are constructed at program startup, and so they
  106. ensure that whatever subsystem is iplied by the header has been
  107. initialized before any application code is executed.
  108.  
  109. My guess is that when <afx.h> is included, objects of this type are
  110. instantiated, and that requires that the whole AFX/MFC subsystem be 
  111. linked in.  If you don't really need MFC, then don't include this.
  112. If you need MFC, then link in the MFC libraries.
  113.  
  114. Hope this helps -- I haven't checked it thoroughly.
  115.  
  116. john lilley
  117.  
  118.